package cc.risingsun.wing.core;

import java.util.Map;

import cc.risingsun.wing.core.scanner.AnnotatedScannerListener;
import cc.risingsun.wing.core.scanner.PackageNamesScanner;
import cc.risingsun.wing.core.scanner.Scanner;
import cc.risingsun.wing.core.scanner.ScannerListener;
import cc.risingsun.wing.core.visitor.DataAccessInterfaceVisitor;
import cc.risingsun.wing.dai.C3P0DataSourceFactory;
import cc.risingsun.wing.ds.DataStruct;


/**
 * 
 * @author iron.yin@gmail.com
 * @sina 2012-9-5
 */

public class WingContext {

	private String packages;
	private final WingFactory factory;
	private Map<Class<?>,Object> map = DataStruct.hashMap();
	
	public WingContext(String packages)throws Exception {
		this.packages=packages;
		factory = new WingFactory(this);
		init();
		initClass();
		factory.process();
	}
	
	void initClass() throws Exception{
		Class.forName(C3P0DataSourceFactory.class.getName());
	}
	
	void init(){
		AnnotatedClassVisitor visitor = new DataAccessInterfaceVisitor();
		ScannerListener listener = new AnnotatedScannerListener(visitor);
		Scanner scanner = new PackageNamesScanner(packages);
		scanner.scan(listener);
	}
	
	void putBean(Class<?> clazz,Object obj){
		map.put(clazz, obj);
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getBean(Class<T> clazz){
		return (T)map.get(clazz);
	}
}
